package com.tanhua.manager.listeners;

import com.alibaba.fastjson.JSON;
import com.tanhua.autoconfig.templates.AliyunGreenTemplate;
import com.tanhua.domain.db.Log;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.dubbo.api.mongo.MovementApi;
import com.tanhua.manager.service.LogService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MovementLinstener {

    @DubboReference
    private MovementApi movementApi;

    
    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;
//    1、声明队列 2、声明交换机 3、绑定队列和交换机
// rabbitTemplate.convertAndSend("movement.exchange","movement.save",movementId);


    @RabbitListener(bindings=@QueueBinding(value = @Queue(name="movement.check"),exchange = @Exchange(name = "movement.exchange",type = ExchangeTypes.DIRECT),key = "movement.save"))
    public void movementCheck(String movementId) throws Exception{

        Movement movement = movementApi.queryMovementsById(movementId);
        if(movement==null || movement.getState()!=0){ // 1通过 2没有通过

            return;
        }

        Map<String, String> testMap = aliyunGreenTemplate.greenTextScan(movement.getTextContent());
        Map imageMap = aliyunGreenTemplate.imageScan(movement.getMedias());

        if(testMap.get("suggestion").equals("pass") && imageMap.get("suggestion").equals("pass") ){
//            更新动态的状态 state=1   update table set state=?? where id=??
            movementApi.updateStateById(1,movementId);
        }else{
//            更新动态的状态 state=2
            movementApi.updateStateById(2,movementId);
        }

        System.out.println("已经完成对动态的审核...............");


    }



}
